package com.enumTest;

/**
 * <p>Description: [用法四：覆盖枚举的方法]</p>
 * Created on 2021/1/8
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class test4 {
    public static void main(String[] args) {
        System.out.println(Color4.values());
        for (Color4 c : Color4.values()) {
            System.out.println(c);
        }
    }
}

enum Color4 {
    RED("红色", 1), GREEN("绿色", 2), BLANK("白色", 3), YELLO("黄色", 4);
    // 成员变量
    private String name;
    private int index;

    // 构造方法
    private Color4(String name, int index) {
        this.name = name;
        this.index = index;
    }

    // 普通方法
    public static String getName(int index) {
        for (Color4 c : Color4.values()) {
            if (c.getIndex() == index) {
                return c.name;
            }
        }
        return null;
    }

    //覆盖方法
    @Override
    public String toString() {
        return this.index + "_" + this.name;
    }

    // get set 方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
